﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Antioch.Base
{
    public class StickMenu
    {
        public int Place { get; private set; }
        int StickMovements = 0;
		public float StickRate = 0.3f;
        int Count = 0;

        public StickMenu(int Count)
        {
            this.Count = Count;
        }

         void DecPlace()
        {
            --Place;
            if (Place < 0) Place = Count - 1;
        }

        void IncPlace()
        {
            ++Place;
            if (Place >= Count) Place = 0;
        }

        public void Update(EventBasedInput.ButtonInfo Info)
        {
            Update(Info.LastEvent, Info.StickPosition, Info.Duration);
        }

		public void Update(EventBasedInput.EventTypes type, Vector2 delta, TimeSpan duration)
		{

			if (type == EventBasedInput.EventTypes.Pressed)
			{
				StickMovements = 0;
				if (delta.Y < 0) IncPlace();
				else DecPlace();
			}
			else if (type != EventBasedInput.EventTypes.None)
			{
				if (delta.Y < 0)
				{
					if (StickMovements > 0) StickMovements = 0;
					float TimeSoFar = -StickMovements * StickRate;
					if (duration.TotalSeconds - TimeSoFar > StickRate)
					{
						IncPlace();
						--StickMovements;
					}
				}
				else
				{
					if (StickMovements < 0) StickMovements = 0;
					float TimeSoFar = StickMovements * StickRate;
					if (duration.TotalSeconds - TimeSoFar > StickRate)
					{
						DecPlace();
						++StickMovements;
					}


				}
			}
		}
    }
}
